//
//  GAOStatusesCell.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

//  子控件之间的间距
let StatusTableViewCellMargin: CGFloat = 10

// 首页微博cell
class GAOStatusesCell: UITableViewCell {
    
    //接收要改动的Toolbar顶部约束
    var toolBarTop: Constraint?
    
    var statusesViewModel: GAOStatusesViewModel? {
        didSet {
            //给原创微博视图设置数据
            originalView.statusesViewModel = statusesViewModel
            //给Toolbar视图设置数据
            toolBar.statusesViewmModel = statusesViewModel
            //卸载顶部约束
            toolBarTop?.uninstall()
            
            //判断是否有转发内容
            if statusesViewModel?.statuses?.retweeted_status != nil {
                //给转发微博视图设置数据
                repostView.statusesViewModel = statusesViewModel
                repostView.hidden = false
                
                toolBar.snp_makeConstraints { (make) -> Void in
                    //记录Toolbar的顶部约束
                    self.toolBarTop = make.top.equalTo(repostView.snp_bottom).constraint
                }
                
            }else {
                //没有转发内容,隐藏repostView,并更新toolBar的约束
                repostView.hidden = true
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    //记录Toolbar的顶部约束
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    
    //原创微博视图
    private lazy var originalView: GAOStatusesOriginalView = GAOStatusesOriginalView()
    
    //微博底部ToolBar
    private lazy var toolBar: GAOStatusesToolBarView = GAOStatusesToolBarView()
    
    //转发微博视图
    private lazy var repostView: GAOStatusesRepostView = GAOStatusesRepostView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(repostView)
        contentView.addSubview(toolBar)

        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top).offset(8)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
        }
        
        repostView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            //记录Toolbar的顶部约束
            self.toolBarTop = make.top.equalTo(repostView.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(40)
        }
        //设置contentView的底部等于最下面控件的底部,从而达到自适应行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
        }
        
    }
    
}
